package manager;


import java.io.File;
import java.util.HashMap;
import java.util.Map;

import entity.BiddingEntity;
import entity.CommondityListEntity;
import entity.DeleteEntity;
import entity.ForgetPwdEntity;
import entity.LoginEntity;
import entity.PublicEntity;
import entity.RegisterEntity;
import entity.SavePictureEntity;
import entity.UpdateCommodityInfoEntity;
import entity.UserJoinCommodity;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Observable;
import rx.functions.Func1;
import utils.LogUtils;

/**
 * Created by Mr.Q on 2018/4/13.
 * 描述
 */

public class ApiRequestManager {

    private ApiRequestManager() {
    }

    private volatile static ApiRequestManager instance;

    public static ApiRequestManager getInstance() {
        if (instance == null) {
            synchronized (ApiRequestManager.class) {
                if (instance == null) {
                    instance = new ApiRequestManager();
                }
                return instance;
            }
        }
        return instance;
    }

    /**
     * 注册用户
     *
     * @param userNum
     * @param password
     * @return
     */
    public Observable<RegisterEntity> register(String userNum, String password, String headPortraitPath, String nickName, String sex) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .register(userNum, password, nickName, headPortraitPath, sex)
                .map(new Func1<RegisterEntity, RegisterEntity>() {
                    @Override
                    public RegisterEntity call(RegisterEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<RegisterEntity>io_main());
    }

    /**
     * 修改用户
     *
     * @param userNum
     * @param password
     * @return
     */
    public Observable<LoginEntity> modify(String userNum, String password, String headPortraitPath, String nickName, String sex, int id) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .modify(userNum, password, nickName, headPortraitPath, sex, id)
                .map(new Func1<LoginEntity, LoginEntity>() {
                    @Override
                    public LoginEntity call(LoginEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<LoginEntity>io_main());
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    public Observable<SavePictureEntity> uploadFile(File file) {

        Map<String, RequestBody> map = new HashMap<>();
        RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        LogUtils.logd(file.getAbsolutePath());

        if (file.getAbsolutePath().endsWith(".jpg")) {
            map.put("file\"; filename=\"file.jpg", fileBody);
        } else if (file.getAbsolutePath().endsWith(".amr")) {
            map.put("file\"; filename=\"file.amr", fileBody);
        } else if (file.getAbsolutePath().endsWith(".png")) {
            map.put("file\"; filename=\"file.png", fileBody);
        }
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .uploadFile(map)
                .map(new Func1<SavePictureEntity, SavePictureEntity>() {
                    @Override
                    public SavePictureEntity call(SavePictureEntity savePictureEntity) {
                        if (savePictureEntity.getMsg().equals("操作成功")) {
                            return savePictureEntity;
                        }
                        throw new ApiException(Integer.valueOf(savePictureEntity.getCode()), savePictureEntity.getMsg());
                    }
                });
    }


    /**
     * 用户登录
     *
     * @param userNum
     * @param password
     * @return
     */
    public Observable<LoginEntity> login(String userNum, String password) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .login(userNum, password).map(new Func1<LoginEntity, LoginEntity>() {
                    @Override
                    public LoginEntity call(LoginEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<LoginEntity>io_main());
    }

    /**
     * 忘记密码
     *
     * @param userNum
     * @param password
     * @return
     */
    public Observable<ForgetPwdEntity> forgetPwd(String userNum, String password) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .forgetPwd(userNum, password).map(new Func1<ForgetPwdEntity, ForgetPwdEntity>() {
                    @Override
                    public ForgetPwdEntity call(ForgetPwdEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<ForgetPwdEntity>io_main());
    }

    /**
     * 竞价
     */
    public Observable<BiddingEntity> bidding(String userId, String commodityId) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .bidding(userId, commodityId).map(new Func1<BiddingEntity, BiddingEntity>() {
                    @Override
                    public BiddingEntity call(BiddingEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<BiddingEntity>io_main());
    }

    /**
     * 发布商品
     */
    public Observable<PublicEntity> publishCommodity(int userId, String picPath, String title, String startTime, String endTime, String startPrice, String priceGradient, String des) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .publishCommodity(userId, picPath, title, startTime, endTime, startPrice, priceGradient, des)
                .map(new Func1<PublicEntity, PublicEntity>() {
                    @Override
                    public PublicEntity call(PublicEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<PublicEntity>io_main());
    }

    /**
     * 发布商品
     */
    public Observable<UpdateCommodityInfoEntity> updateCommodityInfo(int commodityId, int userId, String picPath, String title, String startTime, String endTime, String startPrice, String priceGradient, String des) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .updateCommodityInfo(commodityId,userId, picPath, title, startTime, endTime, startPrice, priceGradient, des)
                .map(new Func1<UpdateCommodityInfoEntity, UpdateCommodityInfoEntity>() {
                    @Override
                    public UpdateCommodityInfoEntity call(UpdateCommodityInfoEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<UpdateCommodityInfoEntity>io_main());
    }

    /**
     * 获取商品列表
     */
    public Observable<CommondityListEntity> getCommodityList(int page, int rows, String state) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .getCommodityList(page, rows, state)
                .map(new Func1<CommondityListEntity, CommondityListEntity>() {
                    @Override
                    public CommondityListEntity call(CommondityListEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<CommondityListEntity>io_main());
    }

    /**
     * 获取自己发布的商品列表
     */
    public Observable<CommondityListEntity> getMyPublishList(int page, int rows, int userId) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .getMyPublishList(page, rows, userId)
                .map(new Func1<CommondityListEntity, CommondityListEntity>() {
                    @Override
                    public CommondityListEntity call(CommondityListEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<CommondityListEntity>io_main());
    }

    /**
     * 获取自己发布的商品列表
     */
    public Observable<CommondityListEntity> getCommodityStateList(int page, int rows, int userId, String state) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .getCommodityStateList(page, rows, userId, state)
                .map(new Func1<CommondityListEntity, CommondityListEntity>() {
                    @Override
                    public CommondityListEntity call(CommondityListEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<CommondityListEntity>io_main());
    }
    /**
     * 获取自己参与过得竞品列表
     */
    public Observable<UserJoinCommodity> userJoinCommodity(String userId) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .userJoinCommodity(userId)
                .map(new Func1<UserJoinCommodity, UserJoinCommodity>() {
                    @Override
                    public UserJoinCommodity call(UserJoinCommodity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<UserJoinCommodity>io_main());
    }

    /**
     * 删除商品id
     */
    public Observable<DeleteEntity> deleteCommodity(int id) {
        return ApiManager.getApiService(HostType.MOBILE_NURSE)
                .deleteCommodity(id)
                .map(new Func1<DeleteEntity, DeleteEntity>() {
                    @Override
                    public DeleteEntity call(DeleteEntity baseResult) {
                        if (baseResult.getMsg().equals("操作成功")) {
                            return baseResult;
                        }
                        throw new ApiException("0", "请求失败");
                    }
                }).compose(RxSchedulers.<DeleteEntity>io_main());
    }


//
//    /**
//     * 获取票务信息列表
//     *
//     * @param startSite
//     * @param endSite
//     * @param startTime
//     * @param mPageNow
//     * @param mRowSize
//     * @return
//     */
//    public Observable<TicketInfoEntity> getTicketList(String startSite, String endSite, String startTime, int mPageNow, int mRowSize) {
//        return ApiManager.getApiService(HostType.MOBILE_NURSE)
//                .getTicketList(startSite, endSite, startTime, mPageNow, mRowSize)
//                .map(new Func1<TicketInfoEntity, TicketInfoEntity>() {
//                    @Override
//                    public TicketInfoEntity call(TicketInfoEntity baseResult) {
//                        if (baseResult.getMsg().equals("操作成功")) {
//                            return baseResult;
//                        }
//                        throw new ApiException(baseResult.getCode(), "操作失败");
//                    }
//                }).compose(RxSchedulers.<TicketInfoEntity>io_main());
//    }
//
//    /**
//     * 提交订单接口
//     *
//     * @param id
//     * @param ticketType
//     * @return
//     */
//    public Observable<SubmitOrderEntity> submitOrder(String id, int ticketType) {
//        return ApiManager.getApiService(HostType.MOBILE_NURSE)
//                .submitOrder(id, ticketType, 1).map(new Func1<SubmitOrderEntity, SubmitOrderEntity>() {
//                    @Override
//                    public SubmitOrderEntity call(SubmitOrderEntity baseResult) {
//                        if (baseResult.getMsg().equals("操作成功")) {
//                            return baseResult;
//                        }
//                        throw new ApiException("0", "请求失败");
//                    }
//                }).compose(RxSchedulers.<SubmitOrderEntity>io_main());
//    }
//
//    /**
//     * 提交订单接口
//     *
//     * @param userId
//     * @param state
//     * @return
//     */
//    public Observable<TravelEntity> getNoTravelOrder(String userId, String state) {
//        return ApiManager.getApiService(HostType.MOBILE_NURSE)
//                .getNoTravelOrder(userId, state).map(new Func1<TravelEntity, TravelEntity>() {
//                    @Override
//                    public TravelEntity call(TravelEntity baseResult) {
//                        if (baseResult.getMsg().equals("操作成功")) {
//                            return baseResult;
//                        }
//                        throw new ApiException("0", "请求失败");
//                    }
//                }).compose(RxSchedulers.<TravelEntity>io_main());
//    }
//
//    /**
//     * 生成出行订单
//     *
//     * @return
//     */
//    public Observable<OrderSureEntity> getOrderSure(String id, String ticketId, int num, String ticketType) {
//        return ApiManager.getApiService(HostType.MOBILE_NURSE)
//                .getOrderSure(id, ticketId, num, ticketType).map(new Func1<OrderSureEntity, OrderSureEntity>() {
//                    @Override
//                    public OrderSureEntity call(OrderSureEntity baseResult) {
//                        if (baseResult.getMsg().equals("操作成功")) {
//                            return baseResult;
//                        }
//                        throw new ApiException("0", "请求失败");
//                    }
//                }).compose(RxSchedulers.<OrderSureEntity>io_main());
//    }
//
//    /**
//     * 更新订单状态
//     *
//     * @return
//     */
//    public Observable<UpdateTicketState> updateTicketState(int id, String state) {
//        return ApiManager.getApiService(HostType.MOBILE_NURSE)
//                .updateTicketState(id, state).map(new Func1<UpdateTicketState, UpdateTicketState>() {
//                    @Override
//                    public UpdateTicketState call(UpdateTicketState baseResult) {
//                        if (baseResult.getMsg().equals("操作成功")) {
//                            return baseResult;
//                        }
//                        throw new ApiException("0", "请求失败");
//                    }
//                }).compose(RxSchedulers.<UpdateTicketState>io_main());
//    }
}
